/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.strategy.app;

import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.strategy.dto.StrategyContractConditionDTO;
import com.pig4cloud.pigx.strategy.service.StrategyContractService;
import com.pig4cloud.pigx.strategy.vo.StrategyContractAppDetailVO;
import com.pig4cloud.pigx.strategy.vo.StrategyContractAppVO;
import com.pig4cloud.pigx.strategy.vo.StrategyContractProfitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 策略-合约 app端
 *
 * @since 2022-11-24 10:39:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app/contract")
@Api(value = "strategyContract", tags = "策略-合约管理")
public class AppStrategyContractController {

    private final StrategyContractService strategyContractService;

    /**
     * app端查询列表
     *
     * @param condition 查询条件
     * @return {@link R< List< StrategyContractAppVO>>}
     * @since 2022/11/24 10:48
     */
    @ApiOperation(value = "app端查询列表", notes = "app端查询列表")
    @GetMapping("/list")
    public R<List<StrategyContractAppVO>> list(StrategyContractConditionDTO condition) {
        PigxUser user = SecurityUtil.getLoginUserNotNull();
        condition.setUserId(user.getId());
        return R.ok(strategyContractService.appList(condition));
    }

    /**
     * 查询当前策略
     *
     * @return {@link R< StrategyContractAppDetailVO>}
     * @since 2022/11/24 11:21
     */
    @ApiOperation(value = "查询当前策略", notes = "查询当前策略")
    @GetMapping("/current")
    public R<StrategyContractAppDetailVO> current() {
        PigxUser user = SecurityUtil.getLoginUserNotNull();
        return R.ok(strategyContractService.appCurrent(user.getId()));
    }

    /**
     * 开启策略
     *
     * @param id id
     * @return {@link R< Object>}
     * @since 2022/11/24 11:30
     */
    @ApiOperation(value = "开启策略", notes = "开启策略")
    @SysLog("开启策略")
    @PostMapping("/open/{id}")
    public R<Object> open(@PathVariable("id") Long id) {
        strategyContractService.appOpen(id);
        return R.ok();
    }

    /**
     * 关闭策略
     *
     * @param id id
     * @return {@link R< Object>}
     * @since 2022/11/24 11:30
     */
    @ApiOperation(value = "关闭策略", notes = "关闭策略")
    @SysLog("关闭策略")
    @PostMapping("/close/{id}")
    public R<Object> enable(@PathVariable("id") Long id) {
        strategyContractService.appClose(id);
        return R.ok();
    }

    /**
     * 查询用户利润
     *
     * @return {@link R< StrategyContractProfitVO>}
     * @since 2022/11/24 11:30
     */
    @ApiOperation(value = "查询用户利润", notes = "查询用户利润")
    @GetMapping("/profit")
    public R<StrategyContractProfitVO> profit() {
        return R.ok(strategyContractService.profit());
    }

}
